<!-- MAIN -->
<div class="main">
    <!-- MAIN CONTENT -->
    <div class="main-content">
        <div class="container-fluid">
            <!-- OVERVIEW -->
            <div class="panel panel-headline">
                <div class="panel-heading">
                    <h3 class="panel-title">用户管理</h3>
                </div>
                <!-- TABLE STRIPED -->
                <div class="panel">
                    <button id="add_user" type="button" class="btn btn-primary btn-lg" style="float:right; margin-right: 20px">新增用户</button>
                    <button id="add_user" type="button" class="btn btn-primary btn-lg" style="float:right; margin-right: 20px" onclick="show_face_detect_dialog()">用户识别</button>
                    <div class="panel-body">
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>姓名</th>
                                <th>工号</th>
                                <th>email</th>
                                <th>电话</th>
                                <th>所属组织</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="user_tbody"> </tbody>
                        </table>
                    </div>
                </div>
                <!-- END TABLE STRIPED -->
            </div>
            <!-- END OVERVIEW -->
        </div>
    </div>
    <!-- END MAIN CONTENT -->
</div>
<!-- END MAIN -->
<script language="JavaScript">
    $().ready(function(){
        list_users()
        $("#add_user").click(function(){
            $('#myModal').load("./modal_user_add.html", function(){
                $(function () {
                    $('#myModal').modal({
                        keyboard: false,
                        backdrop:'static'
                    })
                });
            })
        })
    })

    function del_user_info(uid){
        if(confirm("用户删除后，所有的与用户有关的信息包括人脸信息均会被删除，确定要删除该用户吗？")){
            var url = "user/" + uid;
            $.ajax( {
                type : "DELETE",
                url : url,
                contentType: "application/json",
                error: function( XMLResponse ) {
                    alert( XMLResponse.responseText )
                },
                success : function ( json ) {
                    if ("success" != json.status) {
                        alert( json.message );
                    }else{
                        load_content_page("/face_recognition/main_user_list")
                    }
                }
            } );
        }
    }
    var target_uid = null;
    function face_collect(uid){
        target_uid = uid;
        // 先查询是否已经存在用户的人脸信息
        var url = "user/" + uid;
        $.ajax( {
            type : "GET",
            url : url,
            contentType: "application/json",
            error: function( XMLResponse ) {
                alert( XMLResponse.responseText )
            },
            success : function ( json ) {
                if ("success" != json.status) {
                    alert( json.message );
                }else{
                    if(json.data != null && json.data.face_count > 0 ){
                        if(confirm("该用户已存在个人脸信息，重新录入会删除已存在的数据，是否继续录入人脸信息？")){
                            var url = "face/truncate/" + uid;
                            $.ajax( {
                                type : "GET",
                                url : url,
                                contentType: "application/json",
                                error: function( XMLResponse ) {
                                    alert( XMLResponse.responseText )
                                },
                                success : function ( json ) {
                                    if ("success" != json.status) {
                                        alert( json.message );
                                    }else{
                                        show_face_collect_dialog()
                                    }
                                }
                            } );
                        }
                    }else{
                        show_face_collect_dialog()
                    }
                }
            }
        } );
    }

    function update_user_info(uid){
        target_uid = uid;
        $('#myModal').load("./modal_user_update.html", function(){
            $(function () {
                $('#myModal').modal({
                    keyboard: false,
                    backdrop:'static'
                })
            });
        } );
    }

    function show_face_collect_dialog(){
        $('#myModal').load("./main_face_collect.html", function(){
            $(function () {
                $('#myModal').modal({
                    keyboard: false,
                    backdrop:'static'
                })
            });
        })
    }
    function show_face_detect_dialog(){
        $('#myModal').load("./main_face_detect.html", function(){
            $(function () {
                $('#myModal').modal({
                    keyboard: false,
                    backdrop:'static'
                })
            });
        })
    }

    function list_users(){
        var url = "user/list";
        var data = {
            "key" : ""
        }
        $.ajax( {
            type : "PUT",
            url : url,
            dataType : "json",
            contentType: "application/json",
            data : JSON.stringify(data),
            error: function( XMLResponse ) {
                alert( XMLResponse.responseText )
            },
            success : function ( json ) {
                if ("success" != json.status) {
                    alert( json.message );
                }else{
                    show_users(json.data)
                }
            }
        } );
    }
    function show_users(users){
        var html="";
        var i = 0;
        if( users != null && users.length > 0 ){
            $(users).each(function(){
                i++
                html += "<tr>";
                html += "<td>"+i+"</td>";
                html += "<td>"+this.username+"</td>";
                html += "<td>"+this.employee_no+"</td>";
                html += "<td>"+this.email+"</td>";
                html += "<td>"+this.mobile+"</td>";
                html += "<td>"+this.unit_id+"</td>";
                html += "<td>";
                html += "<span onclick='face_collect(\""+this.id+"\")' class='label label-primary' style='cursor:pointer'>采集图像</span> ";
                html += "<span onclick='update_user_info(\""+this.id+"\")' class='label label-warning' style='cursor:pointer'>编辑</span> ";
                html += "<span onclick='del_user_info(\""+this.id+"\")' class='label label-danger' style='cursor:pointer'>删除</span> ";
                html += "</td>";
                html += "</tr>";
            })
        }
        $("#user_tbody").html(html)
    }
</script>
